
package npr.gui;  

import processing.core.*;
import java.util.Timer;
import java.util.TimerTask;



public class TextInputField extends GUI{


    private float textPos;        
    private StringBuffer input;
    private int cursorIndex;
    private float cursorPosition;
    private int cursorColor= defaultFontColor;
    private String label, ending;
    private float labelPos;
    private Timer timer;
    private Task task;
    private Field field;

    class Task extends TimerTask 
    { 
	@Override public void run() 
	{   
	    if(active && enabled){
		if(cursorColor==fontColor) cursorColor=255;
		else cursorColor= fontColor;
	    }
	} 
    }

    public TextInputField(int xpos, int ypos, int width, String input, String label, String ending){
	this(xpos, ypos, width, 22, null, defaultFontSize, input, label, ending);
    }

    public TextInputField(int xpos, int ypos, int width, int height, PFont font, int fontSize, String input, String label, String ending){

	super(xpos, ypos, width, height, font, fontSize);
	this.textPos= textYPos();
	this.labelPos= labelXPos(label);
	cursorIndex= input.length();
	cursorPosition= 0.0f;
	for(int i= 0; i<input.length(); i++){
	    cursorPosition+= this.font.width(input.charAt(i))*fontSize;
	}
	this.input= new StringBuffer(input);
        this.label= label;
        this.ending= ending;
	field= new Field(xpos, ypos, width, height);
	timer= new Timer();
	task= new Task();
	timer.scheduleAtFixedRate(task, 500, 500);
    }

    public void enable(){
	super.enable();
	field.enable();
	cursorColor= defaultFontColor;
    }

    public void disable(){
	super.disable();
	field.disable();
	cursorColor= fontColorDisabled;
    }
    
    public void setInput(String input){
	this.input= new StringBuffer(input);
	cursorPosition= 0;
	if(input.length()<cursorIndex)
	    cursorIndex= input.length();
	for(int i= 0; i<cursorIndex; i++){
	    cursorPosition+=font.width(input.charAt(i))*fontSize;
	}
    }
        
    protected void mousePressed(int x, int y){
	super.mousePressed(x, y);
	if(active){
	    float mouse= x-(xpos+textPos);
	    cursorPosition= 0.0f;
	    cursorIndex= 0;
	    float charWidth;
	    for (int i= 0; i<input.length(); i++){
		charWidth= font.width(input.charAt(i))*fontSize;
		if(mouse>cursorPosition-charWidth/2 &&
		   mouse<=cursorPosition+charWidth/2){
		    break;
		}
		cursorPosition+=charWidth;
		cursorIndex++;
	    }
	}
	else cursorColor= 0;
    }


     protected void keyPressed(char keyChar, int keyCode){

	 super.keyPressed(keyChar, keyCode);
	 if(active && !hasInput){
	    if(keyChar==CODED){ 
		if(keyCode==LEFT && cursorIndex>0){
		    cursorPosition-= font.width(input.charAt(--cursorIndex))*fontSize;
		}
		else if(keyCode==RIGHT && cursorIndex<input.length()){
		    cursorPosition+= font.width(input.charAt(cursorIndex++))*fontSize;
		}
	    }
	    else if(keyChar==BACKSPACE){
		if(cursorIndex>0){
		    cursorIndex--;
		    cursorPosition-= font.width(input.charAt(cursorIndex))*fontSize;
		    input.deleteCharAt(cursorIndex);
		}
	    }
	    else if(keyChar==DELETE){
		if(cursorIndex<input.length()){
		    input.deleteCharAt(cursorIndex);
		}
	    }
	    else{
		float inputWidth= 0.0f;
		for (int i= 0; i<input.length(); i++){
		    inputWidth+= font.width(input.charAt(i))*fontSize;
		}
		if(inputWidth+font.width(keyChar)*fontSize<=width-2*textPos){
		    input.insert(cursorIndex, keyChar);
		    cursorPosition+= font.width(input.charAt(cursorIndex))*fontSize;
		    cursorIndex++;
		}
	    }
	}
    }

    public void draw(){

	if(display){
	    parent.hint(DISABLE_DEPTH_TEST);
	    field.draw();
	    parent.fill(fontColor);
	    parent.textFont(font, fontSize);
	    parent.text(label, xpos-labelPos, ypos+height-textPos);
	    parent.text(input.toString(), xpos+textPos, ypos+height-textPos);
	    parent.text(ending, xpos+width+5, ypos+height-textPos);
	    parent.stroke(cursorColor);
	    parent.line(xpos+textPos+cursorPosition, ypos+height-textPos-font.ascent()*fontSize-1, xpos+textPos+cursorPosition, ypos+height-textPos+1);
	    parent.hint(ENABLE_DEPTH_TEST);
	}
    }

    public String getInput(){
	return input.toString();
    }

    public String consumeInput(){
	hasInput= false;
	return input.toString();
    }
}
